
//
//  DataPickerVie.m
//  MicroAPP
//
//  Created by 陈区 on 13-8-22.
//  Copyright (c) 2013年 陈区. All rights reserved.
//

#import "DatePickerView.h"

@interface DatePickerView()

@property (nonatomic,weak) UILabel * titleLabel; /**<标题*/
@property (nonatomic,copy) DatePickerBlock block; /**<<#mark#>*/
@property (nonatomic,assign) BOOL  isShow; /**<<#mark#>*/
@property (nonatomic,strong) NSDate * currentDate;
@property (nonatomic,strong) ZCTool * toolmanger;
@end

static CGFloat kPickViewH = 255.0f;

@implementation DatePickerView
@synthesize datePicker = m_datePicker;
@synthesize space = m_space;
@synthesize formatTime = m_formatTime;
@synthesize myValue =_myValue;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.transform = CGAffineTransformMakeTranslation(0, 200);
        
        boxView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 40)];
        //        boxView.transform = CGAffineTransformMakeTranslation(0, 200);
        boxView.backgroundColor = [UIColor redColor];
        [self addSubview:boxView];
        
        //关闭按钮和确定按钮
        UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(boxView.bounds), 44)];
        btnView.backgroundColor = KBgColor;
        [boxView addSubview:btnView];
        
        //关闭
        UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 60, 44)];
        [closeBtn setTitle:@"取消" forState:UIControlStateNormal];
        closeBtn.tag = 100;
        closeBtn.layer.cornerRadius = 3;
        [closeBtn.layer setMasksToBounds:YES];
        [closeBtn setTitleColor:KColor_3 forState:UIControlStateNormal];
        [closeBtn.titleLabel setFont:kFont(15)];
        [closeBtn addTarget:self action:@selector(btnCancelClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:closeBtn];
        
        //确定
        UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds)-60, 0, 60, 44)];
        submitBtn.tag = 101;
        submitBtn.layer.cornerRadius = 3;
        [submitBtn.layer setMasksToBounds:YES];
        [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        [submitBtn setTitleColor:KColor_B forState:UIControlStateNormal];
        [submitBtn.titleLabel setFont:kFont(15)];
        [submitBtn addTarget:self action:@selector(btnOKClick:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:submitBtn];
        
        
        self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, KScreenWidth, 200)];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        if (@available(iOS 13.4, *)) {
            self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
        self.backgroundColor=[UIColor whiteColor];
        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        self.datePicker.locale = locale;
        [self.datePicker addTarget: self action: @selector(changes) forControlEvents: UIControlEventValueChanged];
        [self addSubview:self.datePicker];
        NSLog(@"%@",NSStringFromCGRect(self.datePicker.frame));
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, CGRectGetWidth(self.bounds), 0.5)];
        lineView.backgroundColor = KColor_Line;
        [boxView addSubview:lineView];
        
        //动画
        [UIView animateWithDuration:0.3 animations:^{
            self.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
    
    }
    return self;
}

#pragma mark - Button Click Event

- (void)btnCancelClick:(id)sender
{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 200);
    } completion:^(BOOL finished) {
        if ([self.fundelegate respondsToSelector:@selector(didDatePickerViewCancelClicked:)])
        {
            [self.fundelegate didDatePickerViewCancelClicked:self];
        }
    }];
    
}

- (void)btnOKClick:(id)sender
{
    if ([self.fundelegate respondsToSelector:@selector(didDatePickerViewDoneClicked:)])
    {
        [self.fundelegate didDatePickerViewDoneClicked:self];
    }
}

-(void)changes
{
    if (self.formatTime) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        [dateFormatter setDateFormat:self.formatTime];
        
        if (self.datePicker.date)
        {
            NSString *value = [dateFormatter stringFromDate:self.datePicker.date];
            
            self.space.text = value;
        }
    }
}

-(NSDate *)selectDate
{
    return [NSDate getNowDateFromatAnDate:self.datePicker.date];
}

#pragma mark -
#pragma mark - UI修改
+ (__kindof DatePickerView *)showDatePickerViewWithDefaultTitle:(NSString *)title onView:(UIView *)superView andBlock:(DatePickerBlock)block
{
    DatePickerView * pickView = [[DatePickerView alloc] init];
    [pickView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    pickView.backgroundColor = [UIColor whiteColor];
    [superView addSubview:pickView];
    pickView.frame = CGRectMake(0, 0, KScreenWidth, kPickViewH);
    
//    [pickView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.offset(0);
//        make.height.mas_equalTo(kPickViewH);
//    }];
    [pickView initSubViewsWithTitle:title];
    [pickView.datePicker setDate:pickView.currentDate animated:YES];
    pickView.block = block;
    pickView.isShow = YES;
    return pickView;
}
- (void)clickedSureBtn
{
    NSString* dateString = [NSString stringNoUTCDateByFormatString:@"yyyy-MM-dd" date:[self selectDate]];
    if (self.nolimit) {
        if(self.block)self.block(dateString);return;
    }
    // 校验
    double current = [self changeToDoubleValueWithTime:[NSString stringNoUTCDateByFormatString:@"yyyy-MM-dd" date:self.currentDate]];
    double select = [self changeToDoubleValueWithTime:dateString];
    if (select>current) {
        kShowToast(@"最大支持当前日期");return;
    }
    
    if(self.block)self.block(dateString);
}
- (double)changeToDoubleValueWithTime:(NSString *)time
{
    if (time==nil||time.length<=0) return 0;
    NSString * timeStr = [time stringByReplacingOccurrencesOfString:@"-" withString:@""];
    return [timeStr doubleValue];
}
- (void)setTitleName:(NSString *)titleName
{
    _titleName = titleName;
    self.titleLabel.text = titleName;
}
- (void)showPickView
{
    if (self.isShow) return;
    [UIView animateWithDuration:0.25 animations:^{
        self.hidden = NO;
        self.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
    self.isShow = YES;
}
- (void)hiddenPickView
{
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, kPickViewH);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.isShow = NO;
    }];
}
- (void)dissmissPickView
{
    self.isShow = NO;
    [self removeFromSuperview];
}
- (void)initSubViewsWithTitle:(NSString *)title
{
    UIView * topView = [[UIView alloc] init];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(44);
    }];
    // 加入标题&确定按钮
    UILabel * label = [[UILabel alloc] init];
    label.font = kFont(16);
    label.textColor = KColor_1;
    label.text = title;
    [topView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.centerX.equalTo(topView.mas_centerX);
    }];
    self.titleLabel = label;
    
    UIButton * btn = [[UIButton alloc] init];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:KColor_B forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickedSureBtn) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = kFont(15);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 17, 0, 0);
    [topView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.offset(0);
        make.width.mas_equalTo(80);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = KColor_Line;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topView.mas_bottom).offset(0);
        make.height.mas_equalTo(0.5);
    }];
    
    
    
    self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    if (@available(iOS 13.4, *)) {
        self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
    }
    self.backgroundColor=[UIColor whiteColor];
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.locale = locale;
    [self.datePicker addTarget: self action: @selector(changes) forControlEvents: UIControlEventValueChanged];
    [self addSubview:self.datePicker];
    self.datePicker.frame = CGRectMake(0, 44, KScreenWidth, 200);
//    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(lineView.mas_bottom).offset(0);
//        make.left.right.bottom.offset(0);
//        make.width.mas_equalTo(KScreenWidth);
//    }];
}

- (NSDate *)currentDate
{
    if (!_currentDate) {
        _currentDate = [NSDate date];
    }return _currentDate;
}
@end


